#include "ObjMapper.h"

ObjMapper objMapper;


const char* ObjMapper::IdentifierString = "__npactivex_obj";

void ObjMapper::AddObjectMapping(NPObject* obj, NPP instance)
{
	NPIdentifier identify = NPNFuncs.getstringidentifier(IdentifierString);
	NPVariant variant;
	INT32_TO_NPVARIANT(reinterpret_cast<int>(instance), variant);
		
	NPNFuncs.setproperty(instance, obj, identify, &variant);
}

NPP ObjMapper::Lookup(NPObject *obj, NPP instance)
{
	NPIdentifier identify = NPNFuncs.getstringidentifier(IdentifierString);
	NPVariant variant;
	// if (!obj->_class->getProperty(obj, identify, &variant))
	if (!NPNFuncs.getproperty(instance, obj, identify, &variant))
		return NULL;
	int value = NULL;
	if (NPVARIANT_IS_INT32(variant))
	{
		value = NPVARIANT_TO_INT32(variant);
	}
	else if (NPVARIANT_IS_DOUBLE(variant)) {
		value = static_cast<int>(NPVARIANT_TO_DOUBLE(variant));
	}

	if (value) {
		NPP theInst = reinterpret_cast<NPP>(value);
		return theInst;
	}
	return NULL;
}